#include <iostream>
#include <vector>
#include <math.h>

using std::cout;
using std::vector;

class Solution
{
public:
    vector<vector<int>> generateMatrix(int n)
    {
        int num = 1;
        vector<vector<int>> v(n, vector<int>(n, 0)); // 二维数组初始化

        int circle = 0;

        int i, j;
        i = j = 0;
        while (circle < (n / 2))
        {

            // 上边
            while (j < n - 1 - circle)
            {
                v[i][j++] = num++;
            }
            // 右边
            while (i < n - 1 - circle)
            {
                v[i++][j] = num++;
            }

            // 下边
            while (j > circle)
            {
                v[i][j--] = num++;
            }

            // 左边
            while (i > circle)
            {
                v[i--][j] = num++;
            }

            ++i;
            ++j;
            ++circle;
        }

        if (n % 2 != 0)
            v[n / 2][n / 2] = pow(n, 2);
        return v;
    }
};

int main(int argc, char const *argv[])
{
    Solution s;
    vector<vector<int>> v = s.generateMatrix(5);

    for (auto &it1 : v)
    {
        for (auto &it2 : it1)
        {
            cout << it2 << " ";
        }
        cout << "\n";
    }

    getchar();

    return 0;
}
